package com.cskaoyan.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

/**
 * @author stone
 * @date 2022/06/17 11:30
 */
@Component
public class CountTimeInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        // 委托类方法执行之前做一些事情
        long start = System.currentTimeMillis();
        // 执行委托类方法 → method.invoke → methodInvocation.proceed
        Object proceed = methodInvocation.proceed();
        // 委托类方法执行之后做一些事情
        long end = System.currentTimeMillis();
        System.out.println("执行时间为：" + (end - start));

        // 返回值，就是代理对象的方法的返回值
        return proceed;
    }
}
